package com.shbaiche.bifenba.adapter.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.base.BoolValue;
import com.shbaiche.bifenba.model.user.FollowInfoVo;
import com.shbaiche.bifenba.ui.data.DataTeamDetailActivity;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.CircularImage;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.kit.KnifeKit;
import cn.droidlover.xdroidbase.router.Router;


public class MoreFollowListAdapter extends SimpleRecAdapter<FollowInfoVo, MoreFollowListAdapter.ViewHolder> {
    private Context mContext;

    private Map<Integer, BoolValue> isFollowMap;

    public MoreFollowListAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public ViewHolder newViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_more_list;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        FollowInfoVo item = data.get(position);

        Glide.with(mContext).load(item.getLogo()).placeholder(R.drawable.ic_my_head).error(R.drawable.ic_my_head).into(holder.ivLogo);

        holder.name.setText(item.getName() + "");

        if (isFollowMap.get(position).getValue()) {
            holder.click.setImageResource(R.drawable.btn_xuan_press);
        } else {
            holder.click.setImageResource(R.drawable.btn_xuan_normall);
        }


        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).getIsFollow().getValue()) {
                    isFollowMap.put(position, BoolValue.NO);
                } else {
                    isFollowMap.put(position, BoolValue.YES);
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public void setData(List<FollowInfoVo> data) {
        if (isFollowMap == null) {
            isFollowMap = new HashMap<>();
            for (int i = 0; i < data.size(); i++) {
                isFollowMap.put(i, data.get(i).getIsFollow());
            }
        }
        super.setData(data);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.more_follow)
        CircularImage ivLogo;

        @BindView(R.id.more_name)
        TextView name;

        @BindView(R.id.more_click)
        ImageView click;

        public ViewHolder(View itemView) {
            super(itemView);
            KnifeKit.bind(this, itemView);
        }
    }
}
